<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios构建过程</title>
</head>
<body>
    <script>
        //声明构造函数
        function Axios(config){
            this.defaults = config;
            this.interceptors = '拦截器'
        }

        Axios.prototype.request = function(){
            console.log(this);
            console.log('发送请求');
        }

        //声明数组
        let arr = ['get','post','put','delete'];
        //遍历数组
        arr.forEach(method => {
            Axios.prototype[method] = function(){
                return this.request();
            }
        });

        //声明函数
        function createInstance(config){
            //使用 new 实例化一个对象  context.request context.get 
            let context = new Axios(config);
            //通过 request 来创建一个函数
            let instance = Axios.prototype.request.bind(context);
            //将 get post put 方法添加到 instance 函数对象身上
            Object.keys(Axios.prototype).forEach(method => {
                instance[method] = Axios.prototype[method];
            });
            //将 context 属性添加至 instance
            Object.keys(context).forEach(key => {
                instance[key] = context[key];
            });
            return instance;
        }

        let axios = createInstance({method: 'GET'});

        axios.Axios = Axios;
        axios.create = function(config){
            return createInstance(config);
        }


    </script>
</body>
</html> 